from abc import ABC, abstractmethod
from typing import Dict

from forward.api.admin.schemas.setting import SettingProtocolInSchema
from forward.utils.config import ConfigUtil
from forward.utils.tools import ToolsUtil


class SettingProtocolService:
    """政策协议服务实现类"""

    async def detail(self) -> Dict[str, dict]:
        """获取政策协议信息"""
        default_val = '{"name":"","content":""}'
        service = await ConfigUtil.get_val("protocol", "service", default_val)
        privacy = await ConfigUtil.get_val("protocol", "privacy", default_val)
        return {
            "service": ToolsUtil.json_to_map(service) if service else {},
            "privacy": ToolsUtil.json_to_map(privacy) if privacy else {},
        }

    async def save(self, protocol_in: SettingProtocolInSchema):
        """保存政策协议信息"""
        await ConfigUtil.set(
            "protocol", "service", protocol_in.service.model_dump_json()
        )
        await ConfigUtil.set(
            "protocol", "privacy", protocol_in.privacy.model_dump_json()
        )

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
